
/**		
 * 
 *	components.LabelComponent
 *	
 *	@version 1.00 | May 29, 2010
 *	@author Fraser Hobbs
 *  
 **/
 
package mx.pacu.components 
{
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	/**
	 * @author Fraser Hobbs
	 */
	public class LabelComponent extends Component implements IComponent
	{
		private var _textField:TextField = new TextField();
		
		public function LabelComponent()
		{
			super();
			
			addChild(_textField);
			_textField.autoSize = TextFieldAutoSize.LEFT;
		}

		override public function draw() : void
		{
			super.draw();
		}
		
		override public function defaultStyles() : void
		{
			styles.width = 100;
			//styles.height = 25;
			styles.paddingTop = 10;
			styles.paddingBottom = 10;
			styles.paddingLeft = 10;
			styles.paddingRight = 10;
			styles.marginTop = 10;
			styles.marginBottom = 10;
			styles.marginLeft = 10;
			styles.marginRight = 10;
			styles.backgroundColor = 0xFF0000;
		}
		
		public function get text() : String
		{
			return _textField.text;
		}
		
		public function set text(__text : String) : void
		{
			_textField.text = __text;
		}
		
		override protected function drawWidth():void
		{
			if(_styles.width)
			{
				_textField.multiline = true;
				_textField.width = _styles.width;
				
				if(_textField.textWidth > _styles.width)
				{
					_absoluteWidth = _textField.textWidth;
				}
				else
				{
					_absoluteWidth = _styles.width;
				}
			}
			else
			{
				_textField.multiline = false;
				
				_absoluteWidth = _textField.textWidth;
			}
		}
		
		override protected function drawHeight():void
		{
			if(_styles.height)
			{
				if(_textField.textHeight > _styles.height)
				{
					_absoluteHeight = _textField.textHeight;
				}
				else
				{
					_absoluteHeight = _styles.height;
				}
			}
			else
			{
				_absoluteHeight = _textField.textHeight;
			}
		}
		
		override protected function drawPadding():void
		{
			var left:Number = _textField.getCharBoundaries(0) ? _textField.getCharBoundaries(0).left : 0;
			var top:Number = _textField.getCharBoundaries(0) ? _textField.getCharBoundaries(0).top : 0;
			
			if(_styles.paddingLeft)
			{
				_textField.x = _styles.paddingLeft - left;
				_absoluteWidth += _styles.paddingLeft;
			}
			else
			{
				_textField.x = -left;
			}
			
			if(_styles.paddingRight)
			{
				_absoluteWidth += _styles.paddingRight;
			}
			
			if(_styles.paddingTop)
			{
				_textField.y = _styles.paddingTop - top;
				_absoluteHeight += _styles.paddingTop;
			}
			else
			{
				_textField.y = -top;
			}
			
			if(_styles.paddingBottom)
			{
				_absoluteHeight += _styles.paddingBottom;
			}
		}
		
		override protected function drawBackground():void
		{
			if(_styles.backgroundColor)
			{
				_background.graphics.beginFill(_styles.backgroundColor);
				_background.graphics.drawRect(0, 0, _absoluteWidth, _absoluteHeight);
				_background.graphics.endFill();
			}
			else
			{
				_background.graphics.clear();
			}
		}
		
		override protected function drawMargin():void
		{
			if(_styles.marginLeft)
			{
				_content.x = _styles.marginLeft;
				_background.x = _styles.marginLeft;
				_absoluteWidth += _styles.marginLeft;
			}
			
			if(_styles.marginRight)
			{
				_absoluteWidth += _styles.marginRight;
			}
			
			if(_styles.marginTop)
			{
				_content.y = _styles.marginTop;
				_background.y = _styles.marginTop;
				_absoluteHeight += _styles.marginTop;
			}
			
			if(_styles.marginBottom)
			{
				_absoluteHeight += _styles.marginBottom;
			}
		}
	}
}
